﻿namespace Nomo.UnityCoreModule.Nodes
{
    /// <summary>
    /// 界面的子界面容器。
    /// </summary>
    [UnityEngine.DisallowMultipleComponent]
    [UnityEngine.RequireComponent(typeof(UnityEngine.RectTransform))]
    public sealed class NodeChildrenContainer : UnityEngine.EventSystems.UIBehaviour
    {
        internal readonly System.Collections.Generic.List<Node> NodeChildren;

        private NodeChildrenContainer()
        {
            NodeChildren = new System.Collections.Generic.List<Node>();
        }

        /// <inheritdoc />
        protected override void Awake()
        {
            base.Awake();
            RefreshNodeChildren();
        }

        private void OnTransformChildrenChanged()
        {
            RefreshNodeChildren();
        }

        private void RefreshNodeChildren()
        {
            NodeChildren.Clear();
            NodeChildren.AddRange(System.Linq.Enumerable.Where(System.Linq.Enumerable.Select(System.Linq.Enumerable.Cast<UnityEngine.Transform>(transform), e => e.GetComponent<Node>()), e => e != null));
        }
    }
}
